import { fileURLToPath, URL } from "node:url";
import { resolve } from "path";
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
// import vueDevTools from "vite-plugin-vue-devtools";
// 引入自动导入
import AutoImport from "unplugin-auto-import/vite";
// 引入自动注册组件
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
// 引入组件名称
import vueSetupExtend from "vite-plugin-vue-setup-extend";
// 引入windicss
import WindiCSS from "vite-plugin-windicss";
// 页面操作
import { createHtmlPlugin } from "vite-plugin-html";
// https://vite.dev/config/

const root = process.cwd();
const getViteEnv = (mode, target) => {
  return loadEnv(mode, root)[target];
};
export default ({ mode }) => {
  const _project = getViteEnv(mode, "VITE_PROJECT");
  const _port = Number(getViteEnv(mode, "VITE_PORT"));
  const _title = Number(getViteEnv(mode, "VITE_TITLE"));
  const _favicon = getViteEnv(mode, "VITE_APP_FAVICON");

  return defineConfig({
    base: "/" + _project,
    publicDir: "public", // 默认 public
    server: {
      // 服务器端口号
      port: _port,
      // 是否自动打开浏览器
      open: true,
    },
    plugins: [
      vue(),
      vueJsx(),
      WindiCSS(),
      vueSetupExtend(),
      AutoImport({
        // 自动导入vue相关函数 如 ref,reactive toRef 等
        imports: ["vue", "vue-router", "pinia"],
        dts: "src/auto-import.d.ts",
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
      // vueDevTools(),
      createHtmlPlugin({
        minify: true,
        pages: [
          {
            filename: _project,
            entry: `/src/packages/${_project}/main.ts`,
            template: "index.html",
            injectOptions: {
              data: {
                title: _title,
                injectIcoLink: _favicon,
              },
            },
          },
        ],
      }),
    ],

    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    build: {
      minify: "terser", // 默认是 esbuild, 用terser进行更深度的压缩
      terserOptions: {
        compress: {
          drop_console: true, // 删除console
          drop_debugger: true, // 删除debugger
        },
      },
      lib: {
        entry: "./src/packages/services/main.ts",
        name: "myVueLib",
        formats: ["es"],
      },
      rollupOptions: {
        input: "./src/packages/services/main.ts",
        // output: {
          // manualChunks(id) {
            // if (id.includes("node_modules")) {
            
            //   // 将node_modules中的包分割到单独的文件中
            //   return id
            //     .toString()
            //     .split("node_modules/")[1]
            //     .split("/")[0]
            //     .toString();
            // }
          // },
        // },
      },
      cssCodeSplit: true, // css 与对应的js 代码块仪器分割
      emptyOutDir: true, // 构建前清空输出目录
      sourcemap: false,
      assetsDir: "assets", // 默认 assets
      outDir: `dist/${_project}`, // 默认 dist
    },
  });
};
